package solutions.ferpa.ztest.rest.action;

import br.com.telescope.adsrt.lib.BaseRuntime;
import br.com.telescope.sql.Filter;
import solutions.ferpa.ztest.ent.*;
import solutions.ferpa.ztest.rest.RestService;
import solutions.ferpa.ztest.rest.model.AssayRequest;
import solutions.ferpa.ztest.rest.model.res.AssayRequestSearchRQ;
import solutions.ferpa.ztest.rest.model.res.AssayRequestSearchRS;

import java.util.ArrayList;

@RestAction(action = "assayRequestSearch", rqType = AssayRequestSearchRQ.class, rsType = AssayRequestSearchRS.class)
public class AssayRequestSearch extends RestService<AssayRequestSearchRQ, AssayRequestSearchRS> {

    public AssayRequestSearch(BaseRuntime ctx) {
        super(ctx);
    }

    @Override
    public AssayRequestSearchRS call() throws Exception {
        AssayRequestSearchRQ rq = bindRequest(AssayRequestSearchRQ.class);
        AssayRequestSearchRS rs = new AssayRequestSearchRS();
        EntSolicitacaoEnsaio solicitacaoEnsaio = new EntSolicitacaoEnsaio(ctx);
        EntModeloHidrometro modelo = solicitacaoEnsaio.joinModeloHidrometro();
        EntPlanoExecucao plano = solicitacaoEnsaio.joinPlanoExecucao();
        EntVazaoPrefixoSerie vazao = modelo.joinVazaoPrefixoSerie();

        Filter filter = new Filter();
        boolean hasFilter = false;

        //TODO: HIGH - Add filter by status;
        if (rq.getId() != null) {
            filter.equal(solicitacaoEnsaio.getId(), rq.getId());
            hasFilter = true;
        }

        // TODO: HIGH - Always set PO code with prefix 'R' plus id, when it is null;
        if (rq.getPoCode() != null) {
            filter.like(solicitacaoEnsaio.getOrdemProducao(), rq.getPoCode());
            hasFilter = true;
        }

        if (hasFilter) solicitacaoEnsaio.filterBy(filter);

        while(solicitacaoEnsaio.next()) {
            AssayRequest assayRequest = new AssayRequest();
            assayRequest.setId(solicitacaoEnsaio.valueOfId());
            assayRequest.setCode("S-" + solicitacaoEnsaio.valueOfId().toString());
            assayRequest.setPoCode(solicitacaoEnsaio.valueOfOrdemProducao());
            assayRequest.setModelCode(modelo.valueOfCodigo());
            assayRequest.setNominalFlow(vazao.valueOfVazaoNominal());
            assayRequest.setAssayPlan(plano.valueOfNome());
            assayRequest.setQty(solicitacaoEnsaio.valueOfQuantidadeHidrometros());
            assayRequest.setQtyApproved(solicitacaoEnsaio.valueOfQuantidadeHidrometrosAprovados());
            assayRequest.setQtyReproved(solicitacaoEnsaio.valueOfQuantidadeHidrometrosReprovados());
            assayRequest.setQtyInAssay(solicitacaoEnsaio.valueOfQuantidadeHidrometrosExecucao());
            assayRequest.setQtyPending(solicitacaoEnsaio.valueOfQuantidadeHidrometrosPendentes());

            if (rs.getResource() == null) {
                rs.setResource(new ArrayList<AssayRequest>());
            }

            rs.getResource().add(assayRequest);
        }

        solicitacaoEnsaio.close();
        return rs;
    }

}
